package com.itheima.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.dao.CategoryMapper;
import com.itheima.utils.ConverterUtil;
import com.itheima.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class categoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Result findPage(Integer page, Integer size, Category searchMap) {

        LambdaQueryWrapper<Category> categoryWrapper = Wrappers.lambdaQuery(Category.class);
        if (searchMap.getName() != null && !searchMap.getName().trim().equals("")) {
            categoryWrapper.eq(Category::getName, searchMap.getName());
        }
        if (searchMap.getIsMenu() != null && !searchMap.getIsMenu().trim().equals("")) {
            categoryWrapper.eq(Category::getIsShow, searchMap.getIsShow());
        }
        if (searchMap.getIsShow() != null && !searchMap.getIsShow().trim().equals("")) {
            categoryWrapper.eq(Category::getIsMenu, searchMap.getIsMenu());
        }
        if (searchMap.getSeq() != null ) {
            categoryWrapper.eq(Category::getSeq, searchMap.getSeq());
        }
        if (searchMap.getParentId() != null ) {
            categoryWrapper.eq(Category::getParentId, searchMap.getParentId());
        }
        IPage<Category> page1 = new Page<>(page, size);
        categoryMapper.selectPage(page1,categoryWrapper);
        //PageBean<Category> pageBean = new ConverterUtil<Category>().convertFromPageInfoToPageBean(page1);
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setRows(page1.getRecords());
        categoryPageResult.setTotal(page1.getTotal());
        return new Result(true,StatusCode.OK,"",categoryPageResult);
    }

    @Override
    public Result addCategory(Category category) {
        categoryMapper.insert(category);
//        this.save(category);
        return new Result(true, StatusCode.OK,"添加成功","");
    }

    @Override
    public Result findById(Integer id) {
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery(Category.class);
        wrapper.eq(Category::getId,id);
        Category one = this.getOne(wrapper);
        if (one==null){
            return new Result(true,StatusCode.ERROR,"查询失败","");
        }
        return new Result(true,StatusCode.OK,"",one);
    }


}
